package com.ylw.project;

import com.ylw.util.Config;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 每个java文件 源码地址，编译之后的地址，以及web目录的地址
 * 把这个对象变为线程安全的，防止多个线程操作改对象出现问题  感觉不是线程安全也没毛病
 */
public class JavaDomain {

    private final static Logger log = Logger.getLogger(JavaDomain.class);

    /**
     * java 路径
     */
    private String sourcePath;
    private List<String> compilePath;
    private List<String> outPutPath;
    private List<String> classNames;
    private String relationPath;
    /**
     *    //wap3.0
     */
    private String projectName;
    /**
     *  //D:\test\wap3.0.war
     */
    private String outputProjectRootPath;
    /**
     *D:\ideaproject1\wap3.0\out\production\wap3.0
     */
    private String compileProjectRootPath;
    private Date startDate;
    private boolean needCompile ;
    private volatile  boolean finishCompile;

    public JavaDomain(String sourcePath) {
        this.sourcePath = sourcePath;
        this.startDate = new Date();
    }

    public void resetClass(){
        classNames =null;
        outPutPath = null;
        compilePath = null;
    }

    public String getSourcePath() {
        if(sourcePath == null){
            log.error("init JavaDomain fail ");
        }
        return sourcePath;
    }

    public List<String> getCompilePath() {
        compilePath = Tools.getAllClassPathForJava(getClassNames(), getCompileProjectRootPath()+getRelationPath());
        return compilePath;
    }

    public List<String> getOutPutPath() {
        outPutPath = Tools.getAllClassPathForJava(getClassNames(), getOutputProjectRootPath()+File.separator+Config.getPropertValue("project.output.class.root").get(0)+getRelationPath());
        return outPutPath;
    }

    public String getProjectName() {
        if(projectName==null){
            projectName = Tools.getProjectName(getSourcePath());
        }
        return projectName;
    }

    public String getOutputProjectRootPath() {
        if(outputProjectRootPath ==null){
            this.outputProjectRootPath = Tools.getOutputProjectRootRealPath(getSourcePath(),(String) Config.getPropertValue("project.source.java.root").get(0));
        }
        return outputProjectRootPath;
    }

    /**
     *\\com\\ct10000\\page\\util
     * @return
     */
    public String getRelationPath() {
        if(relationPath ==null){
            String src = (String) Config.getPropertValue("project.source.java.root").get(0);
            int s = sourcePath.indexOf(src) + src.length();
            String pPath = new File(sourcePath).getParent();
            this.relationPath = pPath.substring(s);
        }
        return relationPath;
    }


    public String getCompileProjectRootPath() {
        if(compileProjectRootPath ==null){
            this.compileProjectRootPath = Tools.getCompileProjectRootRealPath(getSourcePath());
        }
        return compileProjectRootPath;
    }

    public List<String> getClassNames() {
        if(classNames ==null){
            this.classNames = Tools.getAllClassNameForJava(sourcePath);
        }
        return classNames;
    }

    public Date getStartDate() {
        return startDate;
    }

    public boolean isNeedCompile() {
        needCompile = isNeedCompile(this);
        return needCompile;
    }

    /**
     * 根据class文件修改时间和java文件的修改时间判断该java文件是否需要编译
     *
     * @param javaDomain
     * @return
     */
    private boolean isNeedCompile(JavaDomain javaDomain) {
        File javaFile = new File(javaDomain.getSourcePath());
        if(javaDomain.getCompilePath().size() ==0){
            return true;
        }
        File classFile = new File(javaDomain.getCompilePath().get(0));
        if (!classFile.exists() || javaFile.lastModified() > classFile.lastModified()) {
            return true;
        }
        return false;
    }



    public void setNeedCompile(boolean needCompile) {
        this.needCompile = needCompile;
    }



    public boolean isFinishCompile() {
        return finishCompile;
    }



    public void setFinishCompile(boolean finishCompile) {
        this.finishCompile = finishCompile;
    }

    public static void main(String[] args) {
        JavaDomain d  = new JavaDomain("D:\\ideaproject1\\wt_pay\\src\\com\\ct10000\\page\\util\\ResponseCode.java");
        d.getRelationPath();
    }
}
